package checkers.gui.dialogs;

import checkers.common.Texture;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 * Class WaitingDialog
 * Shows waiting gifs and cancel button
 * 
 * @author Cristian Tardivo
 */
public class WaitingDialog extends javax.swing.JDialog {
    private static final ResourceBundle lang = ResourceBundle.getBundle("checkers/common/lang");
    private boolean result;
    
    /**
     * Creates New Waiting Dialog
     * @param parent Parent Frame
     * @param modal Modal exclude
     */
    public WaitingDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        // Waiting gif
        ImageIcon imageIcon = new Texture(Texture.Gifs.load);
        load_gif.setIcon(imageIcon);
        imageIcon.setImageObserver(load_gif); 
        result = true;
        this.getRootPane().setDefaultButton(cancel_butt);
        // Key ESCAPE action
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction(){public void actionPerformed(ActionEvent e){cancel_butt.doClick();}}; 
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
    }

    /**
     * Waiting result
     * @return 
     */
    public boolean getResult(){
        return result;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        load_gif = new javax.swing.JLabel();
        wait_dialog = new javax.swing.JLabel();
        cancel_butt = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(lang.getString("CONECTING")); // NOI18N
        setResizable(false);

        wait_dialog.setText(lang.getString("WAITING_CONECTION")); // NOI18N

        cancel_butt.setText(lang.getString("CANCEL")); // NOI18N
        cancel_butt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_buttActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(54, 54, 54)
                                .addComponent(wait_dialog))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(load_gif, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(cancel_butt)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(wait_dialog)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(load_gif, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addComponent(cancel_butt)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancel_buttActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_buttActionPerformed
        result = false;
        dispose();
    }//GEN-LAST:event_cancel_buttActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel_butt;
    private javax.swing.JLabel load_gif;
    private javax.swing.JLabel wait_dialog;
    // End of variables declaration//GEN-END:variables
}
